﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using IPMessenger.Common.Uitls;
using IPMessenger.Common.Model;

namespace IPMessenger.BroadCast
{
    /// <summary>
    /// Socket广播
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            IPEndPoint iep1 = new IPEndPoint(IPAddress.Broadcast, 9050);  //255,255,255,255: 向世界所有机器发送广播，会遭遇路由器拦截
            string hostName = Dns.GetHostName();

            //IPAddress[] ips = Dns.GetHostAddresses(hostName);  //会获取所有网卡ip，包括虚拟及ipv6
            //byte[] data = Encoding.Default.GetBytes(hostName);

            byte[] data = CommonTools.Serialize<Messenger>(new Messenger(hostName, string.Empty));

            sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
            sock.SendTo(data, iep1);
            sock.Close();

            Console.Read();
        }
    }
}
